﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Main_MyMessage : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    NewsDao nd = new NewsDao();
    NewsVo nv = new NewsVo();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UsersVo uv = new UsersVo();
            uv = (UsersVo)Session["user"];
            if (uv == null)
            {
                Response.Redirect("../index.aspx");
            }
            else
            {
                bd();
            }
        }
    }

    public void bd()
    {
        UsersVo uv = new UsersVo();
        uv = (UsersVo)Session["user"];
        dt = new DataTable();
        dt = nd.getUserNews(uv.Uid.ToString());
        gvnews.DataSource = dt;
        gvnews.DataBind();
        if (dt == null)
        {
            string script = "<script>window.alert('对不起，暂无任何发布信息！');location.href='../index.aspx';</script>";
            Page.RegisterClientScriptBlock("", script);
        }
        if (dt.Rows.Count == 0)
        {
            string script = "<script>window.alert('对不起，暂无任何发布信息！');location.href='../index.aspx';</script>";
            Page.RegisterClientScriptBlock("", script);
        }
        for (int i = 0; i < gvnews.Rows.Count; i++)
        {
            string id = dt.Rows[i][0].ToString();
            id = em.Encript(id);
            HyperLink hl = (HyperLink)gvnews.Rows[i].Cells[0].FindControl("hltitle");
            Label labtime = (Label)gvnews.Rows[i].Cells[0].FindControl("labtime");
            DateTime t = Convert.ToDateTime(labtime.Text);
            labtime.Text = t.ToShortDateString();
            Label labstate = (Label)gvnews.Rows[i].Cells[0].FindControl("labstate");
            if (labstate.Text == "0")
            {
                labstate.Text = "已发布";
            }
            if (labstate.Text == "1")
            {
                labstate.Text = "待审核";
            }
            hl.NavigateUrl = "News.aspx?nid=" + id;
            HyperLink hl2 = (HyperLink)gvnews.Rows[i].Cells[0].FindControl("hlupdate");
            hl2.NavigateUrl = "../UpdateMessage.aspx?nid=" + id;
        }
    }


    protected void gvnews_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvnews.PageIndex = e.NewPageIndex;
        dt = new DataTable();
        dt = nd.getUserNews();
        gvnews.DataSource = dt;
        gvnews.DataBind();
        if (dt == null)
        {
            string script = "<script>window.alert('对不起，暂无任何发布信息！');location.href='../index.aspx';</script>";
            Page.RegisterClientScriptBlock("", script);
        }
        if (dt.Rows.Count == 0)
        {
            string script = "<script>window.alert('对不起，暂无任何发布信息！');location.href='../index.aspx';</script>";
            Page.RegisterClientScriptBlock("", script);
        }
        for (int i = 0; i < gvnews.Rows.Count; i++)
        {
            int size = e.NewPageIndex * 20 + i;
            string id = dt.Rows[size][0].ToString();
            id = em.Encript(id);
            HyperLink hl = (HyperLink)gvnews.Rows[i].Cells[0].FindControl("hltitle");
            Label labtime = (Label)gvnews.Rows[i].Cells[0].FindControl("labtime");
            DateTime t = Convert.ToDateTime(labtime.Text);
            labtime.Text = t.ToShortDateString();
            Label labstate = (Label)gvnews.Rows[i].Cells[0].FindControl("labstate");
            if (labstate.Text == "0")
            {
                labstate.Text = "已发布";
            }
            if (labstate.Text == "1")
            {
                labstate.Text = "待审核";
            }
            hl.NavigateUrl = "News.aspx?nid=" + id;
            HyperLink hl2 = (HyperLink)gvnews.Rows[i].Cells[0].FindControl("hlupdate");
            hl2.NavigateUrl = "../UpdateMessage.aspx?nid=" + id;
        }
    }


    protected void gvnews_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = e.CommandArgument.ToString();
        if (e.CommandName == "del")
        {
            nd.delNews(id);
        }
        bd();
    }
    protected void gvnews_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[5].Attributes.Add("onclick", "return confirm('确认删除吗？')");
        }
    }
    protected void btnreturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("../index.aspx");
    }
}
